package com.dragon.client.controller.auth;

import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.RoleService;
import com.dragon.repository.domain.dto.RoleDTO;
import com.dragon.repository.domain.vo.RoleVO;
import com.dragon.repository.entity.Role;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户角色接口
 * @author Charlotte
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService service;

    @GetMapping("list")
    @ApiOperation("角色列表")
    public ResponseData<List<RoleDTO>> RoleList(@ApiParam(name = "name", value = "角色名称", required = false, type = "String") @RequestParam(required = false) String name){
        List<Role> roleList = service.getRoleList(name);
        return ResponseData.success(RoleDTO.convertList(roleList));
    }

    @PostMapping("add")
    @ApiOperation("添加角色")
    @Log
    public ResponseData addRole(@RequestBody @Validated(Insert.class) RoleVO roleVO){
        service.addRole(RoleVO.convert(roleVO));
        return ResponseData.success();
    }

    @PutMapping("update")
    @ApiOperation("更新角色")
    @Log
    public ResponseData updateRole(@RequestBody @Validated(Update.class) RoleVO roleVO) {
        service.updateRole(RoleVO.convert(roleVO));
        return ResponseData.success();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除角色")
    @Log
    public ResponseData deleteRole(@ApiParam(name = "id",type = "int",required = true,value = "角色id") @RequestParam Integer id) {
        service.deleteRole(id);
        return ResponseData.success();
    }

    @PostMapping("deleteList")
    @ApiOperation("批量删除角色")
    @Log
    public ResponseData deleteRoles(@ApiParam(name = "ids", type = "array", required = true, value = "角色id集合") @RequestBody(required = true) List<Integer> ids){
        service.deleteRoles(ids);
        return ResponseData.success();
    }

    @PostMapping("permission")
    @ApiOperation("权限设置保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "dateType", value = "权限类型", required = true, dataType = "int"),
            @ApiImplicitParam(name = "menuIds", value = "菜单id集合", required = true, dataType = "string"),
            @ApiImplicitParam(name = "departmentIds", value = "部门id集合", required = false, dataType = "string"),
            @ApiImplicitParam(name = "modIds", value = "模块id集合", required = false, dataType = "string")
    })
    public ResponseData rolePermission(@RequestParam Integer roleId, @RequestParam Integer dateType,
                                       @RequestParam String menuIds,@RequestParam(required = false) String departmentIds,
                                       @RequestParam(required = false) String modIds){
        service.rolePermission(roleId,dateType,menuIds,departmentIds,modIds);
        return ResponseData.success();
    }

    @GetMapping("menuList")
    @ApiOperation(value = "角色已有的菜单")
    public ResponseData selectMenuByRoleId(@RequestParam @ApiParam(name = "roleId",required = true,type = "int",value = "角色id") Integer roleId){
        return ResponseData.success(service.selectMenuList(roleId));
    }

    @GetMapping("departmentList")
    @ApiOperation(value = "角色已有的部门")
    public ResponseData selectDepartmentByRoleId(@RequestParam @ApiParam(name = "roleId",required = true,type = "int",value = "角色id") Integer roleId){
        return ResponseData.success(service.selectDepartmentList(roleId));
    }





}
